package com.ys.controller.api.account;

import com.ys.config.MenuId;
import com.ys.entity.finance.TcwPartnerBankInfo;
import com.ys.entity.sys.YsCol;
import com.ys.enume.sys.YsColEnum;
import com.ys.service.base.TcwPartnerBankInfoService;
import com.ys.service.sys.CommonApiService;
import com.ys.service.sys.UploadService;
import com.ys.util.ExportExcelUtils;
import com.ys.vo.LayuiTableVoJA;
import com.ys.vo.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 合作商银行账户管理
 */
@Controller
@RequestMapping("/api/inter/tcwPartnerBankInfo")
public class ApiInterTcwPartnerBankInfoController {
    private static final Logger logger = LoggerFactory.getLogger(ApiInterTcwPartnerBankInfoController.class);

    @Autowired
    private CommonApiService<TcwPartnerBankInfo> commonApiService;

    @Autowired
    private TcwPartnerBankInfoService tcwPartnerBankInfoService;


    @RequestMapping("/search")
    @ResponseBody
    public LayuiTableVoJA search(@ModelAttribute TcwPartnerBankInfo cond,String conditions){
        cond.setOrderby("A.createTime DESC");
        return commonApiService.searchLay(cond);
    }


    @RequestMapping("/searchForModal")
    @ResponseBody
    public LayuiTableVoJA searchForModal(@ModelAttribute TcwPartnerBankInfo cond){
        cond.setOrderby("A.Id DESC");
        cond.setStatus(1);
        return commonApiService.searchLay(cond);
    }

    /**
     * 新增
     */
    @RequestMapping("/toAdd")
    @ResponseBody
    public R toAdd(HttpServletRequest request, @RequestParam Map<String, Object> params
    ) {
        try {
            logger.info("合作商银行账户管理新增传参：" + params);
            //修改
            return tcwPartnerBankInfoService.add(params);
        } catch (Exception e) {
            logger.error("合作商银行账户管理新增出错：", e.getMessage(), e);
            return R.error("系统正在开小差，请联系客服，我们将第一时间为您解决！");
        }
    }


    /**
     * 修改
     */
    @RequestMapping("/toEdit")
    @ResponseBody
    public R toEdit(HttpServletRequest request, @RequestParam Map<String, Object> params
    ) {
        try {
            logger.info("合作商银行账户管理修改传参：" + params);
            //修改
            return tcwPartnerBankInfoService.edit(params);
        } catch (Exception e) {
            logger.error("合作商银行账户管理修改出错：", e.getMessage(), e);
            return R.error("系统正在开小差，请联系客服，我们将第一时间为您解决！");
        }
    }

    @RequestMapping("/export")
    @ResponseBody
    public R export(@ModelAttribute TcwPartnerBankInfo cond, String conditions) throws Exception{

        cond.setPage(0).setSize(999999);
        cond.setOrderby("A.createTime DESC");

        List<Map<String, Object>> listData = cond.queryMap();


        List<YsCol> listField = new YsCol()
                .setMenuId(MenuId.TCW_PartnerBankInfo)
                .setFrontShow(YsColEnum.FrontShow.SHOW.value)
                .setOrderby("A.SortNo asc")
                .queryList();

        ExportExcelUtils<TcwPartnerBankInfo> util = new ExportExcelUtils<TcwPartnerBankInfo>(new TcwPartnerBankInfo());

        String toFilePath = util.exportExcel(listData, listField, "合作商银行账户管理表");
        UploadService.UploadDto dto = commonApiService.uploadLocalToServer(toFilePath);
        return R.ok(dto.getUrl());
    }

    @RequestMapping("/searchFk")
    @ResponseBody
    public LayuiTableVoJA searchFk(@ModelAttribute TcwPartnerBankInfo cond,String conditions){
        cond.where("A.status = 1");
        cond.setOrderby("A.createTime DESC");
        return commonApiService.searchLay(cond);
    }



    //采购合同单调用
    @RequestMapping("/searchModalByContract")
    @ResponseBody
    public LayuiTableVoJA searchModalByContract(@ModelAttribute TcwPartnerBankInfo cond,String conditions){
        cond.addField("(A.Vlxr + '&' +  A.Vgsdh) AS vlxrAndvgsdh");// 联系人&联系方式
        cond.where("A.status = 1");
        cond.setOrderby("A.createTime DESC");
        return commonApiService.searchLay(cond);
    }

    //合作商管理(采购付款调用)
    @RequestMapping("/cgfk")
    @ResponseBody
    public LayuiTableVoJA cgfk(@ModelAttribute TcwPartnerBankInfo cond,String conditions){
        cond.where("A.status = 1");
        cond.setOrderby("A.createTime DESC");
        return commonApiService.searchLay(cond);
    }

}
